home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3543 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.6 KB

  1. Path: atglab.bls.com!Alun.Champion
  2. From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [] overloding vc4.0
  5. Date: 24 Jan 1996 18:53:04 GMT
  6. Organization: Computer People Inc.
  7. Message-ID: <ALUN.CHAMPION.96Jan24135304@g7240065.bridge.bst.bls.com>
  8. References: <NEWTNews.822530713.12255.moti@motisaad.netmanage.co.il>
  9. NNTP-Posting-Host: bstfirewall.bst.bls.com
  10. In-reply-to: Moti Saadon's message of Wed, 24 Jan 96 16:41:53 PDT
  11.  
  12. In article <NEWTNews.822530713.12255.moti@motisaad.netmanage.co.il> Moti Saadon <moti@netmanage.co.il> writes:
  13.  
  14. : Why can't I compile this code at vc++4.0?
  15. : Thanks.
  16.  
  17. Posting the error would help identify the problem.
  18. Anyway, my comments are on lines starting with 'X'
  19.  
  20. Hope it helps
  21. Regards
  22.  
  23.    -A.
  24.  
  25. //----------------------------begin code-----------------
  26.  
  27. : //temp.cpp
  28.  
  29. : #include <stdio.h>
  30. X #include <stdlib.h>        // For EXIT_SUCCESS return from main.
  31.  
  32. : #include <iostream.h>
  33. X This include is not needed as you are using printf
  34.  
  35. : class Tclass{
  36. : public:
  37. :       int temp;
  38. : };
  39.  
  40. : //Range exeption
  41. : class Range {
  42. :       int low, hi, badIndex;
  43. : public:
  44. :       Range (int l, int h, int b)
  45. :       { low = l; hi = h; badIndex = b; }
  46.  
  47. :       void display()
  48. :       { printf("\nRange error {%d, %d} :%d",
  49. :       low, hi, badIndex); }
  50. X Why not use the stream "cout" ??
  51.  
  52. : };
  53.  
  54.  
  55. : template <class Type>
  56. : class Array {
  57. :       Type *array;
  58. :       int size;    //size of the array
  59. : public:
  60. :       Array (int sz)
  61. :       { array = new Type[size = sz];}
  62. :       ~Array () {delete [] array;}
  63. :       Type &operator [] (int i);
  64. : };
  65.  
  66. : //now comes the definition of the subscript operator
  67. : template <class Type>
  68. : Type &Array::operator[] (int i)
  69. X You are probably getting some complaint here like: class Array must be
  70. X qualified with parameter list of instantiations or maybe not, but this
  71. X should be:
  72. X Type &Array<Type>::operator[] (int i)
  73.  
  74. : {
  75. :       if (i<0 || i>=size)
  76. :           throw Range(0, size-1, i);
  77. :       return array[i];
  78. : }
  79.  
  80. : void func(Array<Tclass> &arr)
  81. : {
  82. :       try {
  83. :           for (int i=0; i<5; i++)
  84. :               //do some work with arr[i]
  85. :               arr[i].temp = 0;                
  86.  
  87. :       }
  88. :       catch (Range &r) {
  89. :           // report error condition
  90. :           r.display(); 
  91. :       }
  92. : }
  93.  
  94.  
  95. : void main()
  96. X This should be
  97. X int main(void)
  98.  
  99. : {
  100. :       //define Array of size 50
  101. X This comment lies and is not reflected in the declaration below.
  102. :       Array<Tclass> array(3);
  103. X Obviously you have made this value too small to demonstrate the exception.
  104.  
  105. :       func(array);    
  106.  
  107. X Should probably return something here now that main is declared properly
  108. X        return EXIT_SUCCESS;
  109. : }
  110.  
  111. //------------------------end of code ---------------------
  112.  
  113. -- 
  114. | A.Champion                |
  115.